package com.fat.config;

import com.fat.entity.R;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalException {

    //处理全局异常
    @ExceptionHandler(value = Exception.class) //用来处理指定的异常 value属性：用来指定异常类型
    public ResponseEntity exception(Exception ex) {
        return R.error(ex.getMessage());
    }

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public ResponseEntity handleValidException(MethodArgumentNotValidException e) {
        return R.error(e.getBindingResult().getFieldError().getDefaultMessage());
    }
}
